<?php
namespace app\admin\controller;
use think\Db;
class Card extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['card_status'] = $role;
            }
            if(isset($area)&&$area != ""){
                $map['card_area'] = $area;
            }
            if(isset($number)&&$number != ""){
                $map['card_sum'] = $number;
            }
            if(isset($key)&&$key!="")
            {
                $map['card_number'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('card c')
                ->join('area a','a.district_id = c.card_area','left')
                ->where($map)
                ->count();
            $lists = Db::name('card c')
                ->join('area a','a.district_id = c.card_area','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('card_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $data = Db::name('admin a')
            ->join('area e','a.area = e.district_id')
            ->where(['id'=>['neq',1],'level'=>3])
            ->field('district_id,district')
            ->select ();
        return $this->fetch ("card/index",['data'=>$data]);
    }


    public function cardAdd(){
        if(request()->isPost ()){
            extract (input());
			return json(['code'=>100,'msg'=>"暂未开放"]);
            // Db::startTrans ();
            // try{
                // if(isset($set)){
                    // if($set <= 0){
                        // return json(['code'=>100,'msg'=>"数量不能小于等于0"]);
                    // }
                    // for($i = 0;$i < $set;$i++){
                        // $password = GetRandCode(6);
                        // $param[] = [
                            // 'card_number'=>GetRandCode(8),
                            // 'card_password'=>md5($password),
                            // 'card_unencrypted'=>$password,
                            // 'card_sum'=>$sum,
                            // 'card_area'=>$area,
                            // 'card_add_time'=>date('Y-m-d H:i:s'),
                            // 'card_edit_time'=>date('Y-m-d H:i:s')
                        // ];
                    // }
                    // Db::name('card')->insertAll($param);
                // }else{
                    // $card = Db::name('card')->where('card_number',$number)->find();
                    // if(!empty($card)){
                        // return json(['code'=>100,'msg'=>'卡号已存在']);
                    // }
                    // $param = [
                        // 'card_number'=>$number,
                        // 'card_password'=>md5($password),
                        // 'card_unencrypted'=>$password,
                        // 'card_sum'=>$sum,
                        // 'card_area'=>$area,
                        // 'card_add_time'=>date('Y-m-d H:i:s'),
                        // 'card_edit_time'=>date('Y-m-d H:i:s')
                    // ];
                    // Db::name('card')->insert($param);
                // }
                // Db::commit ();
                // return json(['code'=>200,'msg'=>'添加成功']);
            // }catch (\Exception $e){
                // Db::rollback ();
                // return json(['code'=>100,'msg'=>'添加失败']);
            // }
        }
        $data = Db::name('admin a')
            ->join('area e','a.area = e.district_id')
            ->where(['id'=>['neq',1],'level'=>3])
            ->field('district_id,district')
            ->select ();
        return $this->fetch ('card/add_card',['data'=>$data]);
    }


    public function cardEdit(){
        extract (input());
        if(request()->isPost ()){
			return json(['code'=>100,'msg'=>'暂未开放']);
            // $card = Db::name('card')->where(['card_number'=>$number,'card_id'=>['neq',$id]])->find();
            // if(!empty($card)){
                // return json(['code'=>100,'msg'=>'卡号已存在']);
            // }
            // $param = [
                // 'card_number'=>$number,
                // 'card_password'=>md5($password),
                // 'card_unencrypted'=>$password,
                // 'card_sum'=>$sum,
                // 'card_status'=>1,
                // 'card_area'=>$area,
                // 'card_edit_time'=>date('Y-m-d H:i:s')
            // ];
            // $res = Db::name('card')->where('card_id',$id)->update($param);
            // if($res){
                // return json(['code'=>200,'msg'=>'编辑成功']);
            // }else{
                // return json(['code'=>100,'msg'=>'编辑失败']);
            // }
        }
        $data = Db::name('card')->where('card_id',$id)->find();
        $area = Db::name('admin a')
            ->join('area e','a.area = e.district_id')
            ->where(['id'=>['neq',1],'level'=>3])
            ->field('district_id,district')
            ->select ();
        return $this->fetch ('card/edit_card',['data'=>$data,'area'=>$area]);
    }

    public function cardDel(){
        extract (input());
        $res = Db::name('card')->where('card_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelAdmin(){
        set_time_limit (0);
        extract(input());
        if($ids == "" && $role =="" && $key == "" && $number == "" && $area == ""){
            $data = Db::name('card c')
                ->join('area a','c.card_area = a.district_id','left')
                ->select();
        }
        if($ids != ""){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data = Db::name('card c')
                ->join('area a','c.card_area = a.district_id','left')
                ->where('card_id','in',$ids)
                ->select();
        }else{
            $map = [];
            if($role != ""){
                $map['card_status'] = $role;
            }
            if($number != ""){
                $map['card_sum'] = $number;
            }
            if($area != ""){
                $map['card_area'] = $area;
            }
            if($key!="")
            {
                $map['card_number'] = ['like',"%" . $key . "%"];
            }
            $data = Db::name('card c')
                ->join('area a','c.card_area = a.district_id','left')
                ->where($map)
                ->select();
        }
        if(empty($data)){
            return json(['code'=>100,'msg'=>'无数据']);
        }
        $cellname = [
            ['card_id','ID',15,'LEFT'],
            ['card_number','卡号',15,'LEFT'],
            ['card_unencrypted','密码',15,'LEFT'],
            ['card_sum','额度',20,'LEFT'],
            ['district','地区',15,'LEFT']
        ];
        $res = exportExcel('充值卡列表','card',$cellname,$data);
        return json($res);
    }
}